import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;

public class IFrame
{
	Image image;
	int x;
	int y;

	int w;	// full size
	int h;

	int fw;	// one frame size
	int fh;

	int x1,y1,x2,y2;

	public IFrame(Image image,int x,int y,int w,int h,int fw,int fh)
	{
		this.image = image;
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.fw = fw;
		this.fh = fh;
	}

	void draw(Graphics g,int gx,int gy)
	{
		g.setClip(gx,gy,fw,fh);
		g.drawImage(image,gx - x,gy - y,null); // Graphics.TOP | Graphics.LEFT
	}

	void drawClipped(Graphics g,int gx,int gy,int cx,int cy,int cw,int ch)
	{
		x1 = gx > cx ? gx : cx;
		y1 = gy > cy ? gy : cy;
		x2 = gx + w < (cx + cw) ? gx + w : cx + cw;
		y2 = gy + h < (cy + ch) ? gy + h : cy + ch;
		g.setClip(x1,y1,x2 - x1,y2 - y1);
		g.drawImage(image,gx - x,gy - y,null);
	}

	void clear()
	{
		image = null;
	}

	Image getIcon(int index)
	{
		int iw = 20;
		int ih = 20;
		BufferedImage img1 = new BufferedImage(fw,fh,BufferedImage.TYPE_INT_ARGB);
		Graphics g = img1.getGraphics();
		g.drawImage(image,-index * fw,0,null);

		BufferedImage img = null;
		float kx = (float)(iw - 2) / (float)fw;
		float ky = (float)(ih - 2) / (float)fh;

		if (kx > 1.0f) kx = 1.0f;
		if (ky > 1.0f) ky = 1.0f;

		if (kx > ky) kx = ky;

		AffineTransformOp at = new AffineTransformOp(new AffineTransform(kx,0f,0f,kx,0f,0f),2);
		img = at.filter(img1,img);
		img1 = img;

		int nw = img1.getWidth(null);
		int nh = img1.getHeight(null);

		img = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_ARGB);
		g = img.getGraphics();
		g.setClip(1,1,iw - 2,ih - 2);
		g.drawImage(img1,1 + ((iw - 2 - nw) >> 1),1 + ((ih - 2 - nh) >> 1),null);
		return img;
	}

}

